﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;
using System.Data;
using System.Data.SqlClient;

namespace Oplaco.Web.DTO
{
    public class FeaturedAdversitmentManager
    {
        private const int NumCachedFeatures = 10;
        private const int CacheLifeTime = 10; // in minutes
        private static Random random = new Random();
        private const string CacheKeyPrefix = "FeaturedAdvertismentsCache_";



        public CachedFeaturedAdvertismentCollection Fetch(HttpContext context, int maxitems) {

            CachedFeaturedAdvertismentCollection AdsCollection = null;            
            Oplaco.Core.DAL.AdsDataComponent.AdsDataTable data;

            try
            {

                //custom cache handling
                if (context == null) Oplaco.Exceptions.AppService.ProcessAndNotifyBusinessLayerException(new Exception("Could not find the current HttpContext"));

                int featureCacheNumber = random.Next(0, NumCachedFeatures - 1);
                string cacheKey = GetCacheKey(featureCacheNumber);

                AdsCollection = context.Cache[cacheKey] as CachedFeaturedAdvertismentCollection;
                if (AdsCollection == null)
                {
                    data = AdsDB.GetFeaturedAdsSelection(3);
                    if (data != null && data.Count > 0)
                    {

                        CachedFeaturedAdvertisment[] Ads = (from DataRow row in data.Rows
                                                            select new CachedFeaturedAdvertisment((int)row["Id"], (int)row["PreviewImageId"], row["Title"].ToString(), row["Description"].ToString())).ToArray<CachedFeaturedAdvertisment>();

                        if (Ads.Count() > 0)
                        {
                            AdsCollection = new CachedFeaturedAdvertismentCollection();
                            AdsCollection.CopyTo(Ads, 0);
                            DateTime expiration = DateTime.Now.AddMinutes(CacheLifeTime);
                            context.Cache.Add(cacheKey, AdsCollection, null, expiration, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                        }

                    }
                }
            }
            catch (SqlException SQLEX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(SQLEX);
            }
            catch (Exception EX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyBusinessLayerException(EX);
            }

            return AdsCollection;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="featureCacheNumber"></param>
        /// <returns></returns>
        private static string GetCacheKey(int featureCacheNumber)
        {
            return CacheKeyPrefix + featureCacheNumber.ToString();
        }

    }


    
}
